/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.magri.mvc.tela.binding;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextField;
import org.magri.mvc.modelo.AbstractBean;
import org.magri.mvc.tela.controles.NavBar;

/**
 *
 * @author Magri
 */
public class BindingBean {
    private NavBar navBar;
    private Class<AbstractBean> classBean;
    private AbstractBean bean;
    private Map bindings = new HashMap();

    public BindingBean(Class classBean) {
        this.classBean = classBean;
    }
    
    public void setNavBar(NavBar navBar) {
        this.navBar = navBar;
    }
    
    //public void setBean(AbstractBean bean) {
    //    this.bean = bean;
    //    refresh();
    //}
    
    public void refresh() {
        for (Iterator it = bindings.keySet().iterator(); it.hasNext();) {
            Object key = it.next();
            
        }
    }
    
    public void novo() {
        try {
            bean = classBean.newInstance();
        } catch (Exception ex) {
            Logger.getLogger(BindingBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void addBinding(final JTextField componente, final String field) {
        
        bindings.put(field, componente);
        
        componente.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
                
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (bean != null) {
                    bean.set(field, componente.getText());
                }
            }
        });
        
        componente.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (bean != null) {
                    bean.set(field, componente.getText());
                }
            }
        });
    }
    
}
